package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.util.SpHelper;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.listener.OnItemChangeListener;

import java.util.List;

/**
 * @author xubin
 */
public class RemandSelectedGoodsAdapter extends BaseQuickAdapter<SortBean, BaseViewHolder> {
    private final String locationName = SpHelper.INSTANCE.decodeString(Constants.SpKey.STOREHOUSE_LOCATION_NAME);

    private OnItemChangeListener mOnItemChangeListener;

    public RemandSelectedGoodsAdapter(int layoutResId, @Nullable List<SortBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        helper.setText(R.id.tv_title, item.getSortName())
                .setText(R.id.tv_model, "型号：" + item.getSortModel())
                .setText(R.id.tv_num, "完好：" + item.getTotalReturnNum())
                .setText(R.id.tv_damage_num, "损坏：" + item.getTotalRecordDamageNum())
                .setText(R.id.tv_loc, "位置：" + item.getLocText(SpHelper.INSTANCE.decodeInt(Constants.SpKey.STOREHOUSE_LOCATION_ID), locationName));
        helper.getView(R.id.tv_loc).setOnClickListener(v -> {
            if (getOnItemChildClickListener() != null) {
                getOnItemChildClickListener().onItemChildClick(RemandSelectedGoodsAdapter.this, v, helper.getAbsoluteAdapterPosition());
            }
        });
        helper.getView(R.id.cl_root).setOnClickListener(v -> {
            if (getOnItemChildClickListener() != null) {
                getOnItemChildClickListener().onItemChildClick(RemandSelectedGoodsAdapter.this, v, helper.getAbsoluteAdapterPosition());
            }
        });
        helper.getView(R.id.iv_delete).setOnClickListener(v -> {
            remove(item);
            if (mOnItemChangeListener != null) {
                mOnItemChangeListener.onChange(helper.getLayoutPosition() - getHeaderLayoutCount());
            }
        });
    }

    public void setOnItemChangeListener(OnItemChangeListener onItemChangeListener) {
        mOnItemChangeListener = onItemChangeListener;
    }
}
